package com.app.service.impl;


import com.app.mapper.CusWorkExperienceMapper;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * @Description:项目经验
 */
@Service
public class CusWorkExperienceServiceImpl implements CusWorkExperienceService {
    @Autowired
    UserService userService;
    @Autowired
    CustomerService customerService;
    @Autowired
    CusWorkExperienceService cusWorkExperienceService;
    @Autowired
    CusWorkExperienceMapper cusWorkExperienceMapper;
    @Autowired
    PositionService positionService;

    @Override
    public int insert(CusWorkExperienceModel model) {
        return cusWorkExperienceMapper.insert(model);
    }

    @Override
    public void update(CusWorkExperienceModel model) {
        cusWorkExperienceMapper.updateById(model);
    }

    @Override
    public List<CusWorkExperienceModel> getWorkExpByCustomerId(int customerId) {
        QueryWrapper<CusWorkExperienceModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("customer_id", customerId).orderByAsc("work_time_start");
        List<CusWorkExperienceModel> userList = cusWorkExperienceMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }

    }

    @Override
    public void delete(int id) {
        cusWorkExperienceMapper.deleteById(id);
    }

    @Override
    //根据简历ID查询工作经历
    public List<CusWorkExperienceModel> getWorkExpList(int customerId) {

        QueryWrapper<CusWorkExperienceModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "company_name", "industry", "work_time_start", "work_time_end", "post_name", "post_type", "duty", "work_explain", "shield").eq("customer_id", customerId);
        List<CusWorkExperienceModel> CusWorkExperienceList = cusWorkExperienceMapper.selectList(queryWrapper);
        if (CusWorkExperienceList.isEmpty()) {
            return null;
        } else {
            return CusWorkExperienceList;
        }
    }

    @Override
    //根据ID查询工作经历
    public Object getWorkExp(HttpServletRequest request) {
        String id = request.getParameter("id");
        CusWorkExperienceModel model = cusWorkExperienceMapper.selectById(Integer.valueOf(id));
        PositionModel positionModel = positionService.getModelById(model.getPostType());
        model.setPostTypeText(positionModel.getJobTitle());
        if (model != null) {

            return model;
        }
        return null;
    }

    @Override
    public List<CusWorkExperienceModel> selectExp() throws ParseException {
        List<CusWorkExperienceModel> cusWorkExperienceModels = cusWorkExperienceMapper.selectList(null);
        if (cusWorkExperienceModels != null) {
            for (int i = 0; i < cusWorkExperienceModels.size(); i++) {
                Timestamp time2 = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(cusWorkExperienceModels.get(i).getWorkTimeStart() + "-01").getTime());
                String date2 = DateUtil.yearCompare(time2, CommonHelp.getNowTimestamp());
                cusWorkExperienceModels.get(i).setExp(Integer.parseInt(date2));
            }
        }
        return cusWorkExperienceModels;
    }


//    @Service
//    @Log4j2
//    public class CusWorkExperienceServiceImpl implements CusWorkExperienceService {
//        @Autowired
//        CusWorkExperienceMapper cusWorkExperienceMapper;
//
//        @Override
//        public List<CusWorkExperienceModel> getModelByCusId(int customerId) {
//
//            QueryWrapper<CusWorkExperienceModel> queryWrapper = new QueryWrapper<>();
//            queryWrapper.select().eq("customer_id", customerId);
//            List<CusWorkExperienceModel> userList = cusWorkExperienceMapper.selectList(queryWrapper);
//            if (userList.isEmpty()) {
//                return null;
//            } else {
//                return userList;
//            }
//        }
//    }
}